package com.rain.rain.retrofit.http.test


import androidx.lifecycle.viewModelScope
import com.rain.rain.retrofit.http.base.BaseViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import com.rain.rain.retrofit.http.base.Result
/**
 *@Author: Rain
 *@Date: 2021/9/29 11:35
 *@Description: 登录相关的viewModel
 */
class LoginViewModel : BaseViewModel() {

    private val _loginStateFlow =
        MutableStateFlow<Result<LoginUserData>>(Result.Loading(isLoading = false)) // 都返回
    val loginFlow: SharedFlow<Result<LoginUserData>>
        get() = _loginStateFlow

    fun login(smsVerifyCode: String, username: String) {
        viewModelScope.launch {
            LoginRepository.login(smsVerifyCode, username).request(
                start = { result ->
                    _loginStateFlow.value = result
                },
                completion = { result ->
                    _loginStateFlow.value = result
                }
            ).collect { result ->
                _loginStateFlow.value = result
            }
        }
    }


}